import uni from '@dcloudio/vite-plugin-uni'
import * as path from 'node:path'
import AutoImport from 'unplugin-auto-import/vite'

import { defineConfig, loadEnv } from 'vite'

const pathSrc = path.resolve(__dirname, 'src')

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  console.log('运行模式：', mode, env)
  return {
    resolve: {
      alias: {
        '@': pathSrc
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/styles/variables.scss";',
          javascriptEnabled: true
        }
      }
    },
    define: {},
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: mode === 'production'
        }
      }
    },
    plugins: [
      uni(),
      // 自动导入 API，例如在 script 中就不用导入 ref，就能直接使用
      AutoImport({
        include: [
          /\.[jt]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
          /\.md$/ // .md
        ],
        imports: ['vue'],
        resolvers: [
          // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
          // ElementPlusResolver()
        ],
        // 用于 TS
        dts: path.resolve(pathSrc, 'typings', 'auto-imports.d.ts'),
        vueTemplate: true,
        eslintrc: {
          // 自动生成 .eslintrc-auto-import.json，之后导入 eslint 规则里面，即可消除 ref 等 eslint 报错
          enabled: true
        }
      })
    ]
  }
})
